package nl.hanze.designpatterns.gui;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import nl.hanze.designpatterns.DAO.impl.db.LoginCredentialDAOImpl;
import nl.hanze.designpatterns.DAOFactory.AbstractFactory;
import nl.hanze.designpatterns.DAOFactory.impl.ConcreteFactoryDB;
import nl.hanze.designpatterns.DAOFactory.impl.ConcreteFactoryFile;
import nl.hanze.designpatterns.domain.LoginCredential;

public class Login extends JFrame {
	
	private static final long serialVersionUID = 28904628031844394L;
	JTextField usernamefield;
	JPasswordField passwordfield;
	JRadioButton dbButton;
	JRadioButton fileButton;
	ButtonGroup bg;
	JPanel userpannel;
	JPanel pwdpannel;
	public Login(){
		
		JPanel panel = new JPanel();
		getContentPane().add(panel);
		JPanel fieldpanel = new JPanel();
		panel.setLayout(new GridLayout(3,1));
		fieldpanel.setLayout(new GridLayout(2,2,2,2));
		JLabel usern = new JLabel("Username :");
		JLabel passw = new JLabel("Password :");
		JButton loginbutton = new JButton("Login");
				
		loginbutton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent event) {
				LoginCredential lc = new LoginCredential(usernamefield.getText(),passwordfield.getText());
				LoginCredentialDAOImpl lcdi = new LoginCredentialDAOImpl();
				AbstractFactory af = null;
				if(dbButton.isSelected()){
					af = new ConcreteFactoryDB();
				}
				else if(fileButton.isSelected()){
					af = new ConcreteFactoryFile();
				}
				if(af.checkLogin(lc)){
					System.out.println("loggin in...");
					Mainscreen mainscreen = new Mainscreen(usernamefield.getText());
					mainscreen.setVisible(true);
					setVisible(false);
				}else{
					JOptionPane.showMessageDialog(null,
						    "Please insert the right credentials",
						    "Inane error",
						    JOptionPane.ERROR_MESSAGE);
				}
				
				}
		 
		});
		
		JButton quitbutton = new JButton("Quit");
		
		quitbutton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
			
			
		});
		
		usernamefield = new JTextField("", 20);
		passwordfield = new JPasswordField("", 20);
		dbButton = new JRadioButton("Database");
		dbButton.setActionCommand("db");
		fileButton = new JRadioButton("File");
		fileButton.setSelected(true);
		fileButton.setActionCommand("file");
		bg = new ButtonGroup();
		bg.add(dbButton);
		bg.add(fileButton);
		JPanel insidepannel = new JPanel(new FlowLayout());
		JPanel buttonpannel = new JPanel();
		buttonpannel.setLayout(new GridLayout(1,2));
		buttonpannel.add(dbButton);
		buttonpannel.add(fileButton);
		insidepannel.add(loginbutton);
		insidepannel.add(quitbutton);
		userpannel = new JPanel();
		pwdpannel = new JPanel();
		 userpannel.add(usern);
		 userpannel.add(usernamefield);
		 pwdpannel.add(passw);
		 pwdpannel.add(passwordfield);
		fieldpanel.add(userpannel);
		fieldpanel.add(pwdpannel);
		
		panel.add(buttonpannel);
		panel.add(fieldpanel);
		panel.add(insidepannel);
		this.pack();
		
		setTitle("Login");
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setResizable(false);
		
	}

	public static void main(String[] args){
		// TODO create seperate main method and look and feel
				Login login = new Login();
				login.setVisible(true);
			
		};
	}
	

